package chapter8.HomeWork;

public class HomeWork13 {
    public static void main(String[] args) {
        People[] p = new People[4];
        //新建四个对象
        p[0] = new Student("jack", "男", 20, "121214");
        p[1] = new Student("Tom", "男", 21, "821214");
        p[2] = new Teacher("smith", "女", 30, 223134);
        p[3] = new Teacher("milan", "女", 30, 888134);

        //主函数里为什么不能调用该类的publc方法，必须new个对象呢
        HomeWork13 homeWork13 = new HomeWork13();
        homeWork13.sort(p);

        for (int i = 0; i < p.length; i++) {
            p[i].Play();
        }
        for (int j = 0; j < p.length; j++) {
            homeWork13.call(p[j]);
        }
    }

    //方法将People对象中按年龄排序
    public void sort(People[] people) {
        People temp = null;
        for (int i = 0; i < people.length - 1; i++) {
            for (int j = i; j < people.length; j++) {
                //从高到低排序
                if (people[i].getAge() < people[j].getAge()) {
                    temp = people[i];
                    people[i] = people[j];
                    people[j] = temp;
                }
            }
        }
    }
//根据类型调用不同的类的方法
    public void call(People p) {
        //如果当前数组存的是studen
        if (p instanceof Student) {
            //向下转型
            Student st = (Student) p;
            st.study();
        }
        //如果是Teacher或其子类
        else if (p instanceof Teacher) {
            Teacher th = (Teacher) p;
            th.teach();
        } else {

        }
    }
}
